<template>
  <div class="tips-info magictime" >
    <div class="wid position" title="定位" @click="switchView"></div>
    <div class="wid legend" title="图例" @click="activeLegend"></div>
    <div class="wid maps" title="图层" @click="activeMaps"></div>
    <div class="wid baseMap" @click="activeBaseMaps"></div>
    <!-- <div class="wid complaint" v-if="activeType === 'complaint'">
      <img src="../../../../src/assets/service/legend-heatmap.png">
    </div> -->
    <div class="custom_widgets magictime">
      <img :src="legendImg" :class="{ 'active': showLegend }" />
      <div class="mapsChange" :class="{ 'active': showMaps }">
        <ElCheckboxGroup v-model="checkedNames" class="check-group">
          <el-checkbox
            v-for="(item,index) in checkList"
            :key="index"
            class="wid-check"
            size="default"
            :label="item.code"
            :value="item.code"
            @change="handleCheckedx(item.code, $event)"
            >{{  item.typeName + ' (' + item.count +'台)'}}</el-checkbox
          >
        </ElCheckboxGroup>

      </div>
    </div>


  </div>
</template>

<script setup lang="ts">
import * as mars3d from "mars3d"
import { ref, onMounted } from "vue"
import { useWidget } from "@mars/common/store/widget"
import { ElCheckboxGroup } from "element-plus"
import { listStationType } from "@mars/api/data/index"
import Bus from "@mars/utils/bus"
const eventTarget: mars3d.BaseClass = new mars3d.BaseClass()
const { currentWidget } = useWidget()

const { activate, disable } = useWidget()
const activeType = ref()
const checkList = ref<any[]>([])
const checkedNames = ref<string[]>([])

const legendImg = ref()
const showLegend = ref(false)
const showMaps = ref(false)
const showBaseMaps = ref(false)

const switchView = () => {
  console.log("================switchView=========")
  // eventTarget.fire("switchView")
  Bus.emit("switchView")
}
const activeLegend = () => {
  // showLegend.value = !showLegend.value
  // if (!legendImg.value) {
  //   showLegend.value = false
  // }
  showLegend.value = !showLegend.value && !!legendImg.value
  showMaps.value = false
  showBaseMaps.value = false
  shouwBaseMaps()
}
const activeMaps = () => {
  showMaps.value = !showMaps.value
  showLegend.value = false
  showBaseMaps.value = false
  shouwBaseMaps()
}
const activeBaseMaps = () => {
  showBaseMaps.value = !showBaseMaps.value
  showLegend.value = false
  showMaps.value = false
  shouwBaseMaps()
}

const shouwBaseMaps = () => {
  if (showBaseMaps.value) {
    activate("manage-basemap")
  } else {
    disable("manage-basemap")
  }
}

const loadLegendImg = () => {
  if (activeType.value === "complaint") {
    legendImg.value = new URL(`/src/assets/service/legend-heatmap.png`, import.meta.url).href
    showLegend.value = true
  }
}

const getStationType = () => {
  listStationType().then((res:any) => {
    const arrData = res.data
    console.log("listStationType=======", arrData)
    checkList.value = arrData.slice(0, 6).filter(item => item.code !== "SERVER_SW")
    checkedNames.value = checkList.value.map(item => item.code)
  })
}

const handleCheckedx = async (type:string, event:boolean) => {
  Bus.emit("checkDevices", checkedNames.value)
}
onMounted(async () => {

  activeType.value = currentWidget.data?.type ? currentWidget.data?.type : "station"
  if (activeType.value === "station") {
    getStationType()
    showMaps.value = true
  }

  loadLegendImg()
})
</script>

<style lang="less" scoped>
.tips-info{
  position: fixed;
  right: 0px;
  bottom: 0.52083vw;
  width: 2.91667vw;
  height: 8.85417vw;;
  z-index: 1;
  background-image: url("/src/assets/service/wBg.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  .custom_widgets{
    position: absolute;
    bottom: -0.52083vw;
    right:  2.60417vw;
    img{
      display: none;
      width: 60px;
      height: 280px;
      &.active{
        display: block;
      }
    }
    .mapsChange {
      position: absolute;
      bottom: 0.52083vw;
      right: 0.52083vw;
      padding: 4.64583vw 1.5625vw 0.6vw 1.5625vw;
      width: 12.11458vw;
      height: 14.36875vw;
      // height: 100px;
      background-image: url("/src/assets/service/tucengBack.png");
      background-repeat: no-repeat;
      background-size: 100% 100%;
      flex-direction: column;
      display: none;
      &.active{
        display: block;
      }
    }
  }
}
.magictime{
  animation-duration: 1.5s;
}
.wid{
  width: 2.91667vw;
  height: 2.1875vw;
  cursor: pointer;
  &.position {
    background: url() no-repeat;
    background-position: center 70%;
  }
  &.legend {
    background: url() no-repeat;
    background-position: center 50%;
  }
  &.maps{
    background: url() no-repeat;
    background-position: center 35%;
  }
  &.baseMap {
    background: url() no-repeat;
    background-position: center 35%;
  }
  &.baseMap-a {
      background: url() no-repeat;
      background-position: center 35%;
  }
  &.complaint{
    img{
      width: 40px;
      height: 180px;
    }
  }

}

.wid-check{
  :deep .el-checkbox__label{
      // font-size: 16px;
      font-size: .83333vw !important;
      color: #fff;
    }

}

.el-checkbox-group {
    font-size: 0;
    line-height: 0;
    display: flex
;
    /* align-content: stretch; */
    flex-wrap: wrap;
    /* justify-content: flex-start; */
    height: 100%;
}
</style>
